//
//  ReviewViewController.m
//  cityStar
//
//  Created by joyban on 15-1-28.
//  Copyright (c) 2015年 tiny. All rights reserved.
//

#import "ReviewViewController.h"
#import "UIColor+FlatUI.h"
#import "ReviewCell.h"
#import "SendCommentsViewController.h"
#import "ReviewViewController.h"

NSString *const ReviewCellID = @"ReviewCellID";


@interface ReviewViewController ()
@property (weak, nonatomic) IBOutlet UITableView *ReviewTable;
@property (nonatomic, strong) NSString *commodityID;
@property (nonatomic, strong) NSMutableArray *requestArray;

@end

@implementation ReviewViewController

-(id)initWithCommodityID:(NSString *)commodityID
{
    self = [super init];
    if (self) {
        self.commodityID = commodityID;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"评论";
    [self addBackButton];
    __weak typeof(self)weakSelf = self;
    [self addRightButtonWithTitle:@"发评论" actionBlock:^{
        SendCommentsViewController *sendView = [[SendCommentsViewController alloc]initWithCommodityID:weakSelf.commodityID];
        [weakSelf.navigationController pushViewController:sendView animated:YES];
    }];
    [self setUpTableViews];
}

-(void)viewWillAppear:(BOOL)animated
{
    [self webRequest_GetReviewMessageisMore:NO];
}

-(void)setUpTableViews
{
    [self.ReviewTable registerNib:[ReviewCell nib] forCellReuseIdentifier:ReviewCellID];
}

-(void)webRequest_GetReviewMessageisMore:(BOOL)isMore
{
    NSString *postUrl = [NSString stringWithFormat:@"%@%@",KinterFace_GoodsCommentGoods,self.commodityID];
    [HttpRequestManager getMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        if (success) {
            if (isMore) {
                [self.requestArray addObjectsFromArray:response[@"value"][@"content"]];
            }else{
                self.requestArray = [[NSMutableArray alloc]initWithArray:response[@"value"][@"content"]];
            }
            [self.ReviewTable reloadData];
            DLog(@"%@",response);
            
        }else{
            [SVProgressHUD showErrorWithStatus:errMsg];
        }
    }];
}

#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [ReviewCell heightOfTableCell];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 26;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.requestArray.count;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIImageView *imageTwo = [[UIImageView alloc]initWithFrame:CGRectMake(0, 25, tableView.bounds.size.width, 1)];
    [imageTwo setBackgroundColor:[UIColor colorFromHexCode:@"ECECEC"]];
    
    UILabel * label = [[UILabel alloc] init] ;
    label.frame = CGRectMake(0, 0, tableView.bounds.size.width, 26);
    label.backgroundColor = [UIColor clearColor];
    label.font=[UIFont systemFontOfSize:14];
    label.textColor = [UIColor colorFromHexCode:@"333333"];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = [NSString stringWithFormat:@"共%i条评论",(int)self.requestArray.count];

    UIView * sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 26)] ;
    [sectionView setBackgroundColor:[UIColor colorFromHexCode:@"F6F5F1"]];
    [sectionView addSubview:label];
    [sectionView addSubview:imageTwo];
    return sectionView;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView * sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 1)] ;
    [sectionView setBackgroundColor:[UIColor colorFromHexCode:@"ECECEC"]];
    return sectionView;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReviewCell *reviewCell = [tableView dequeueReusableCellWithIdentifier:ReviewCellID];
    reviewCell.selectionStyle = UITableViewCellSelectionStyleNone;
    ReViewListMessage *reViewMessage = [ReViewListMessage objectFromDictionary:self.requestArray[indexPath.row]];
    [reviewCell configWithMessage:reViewMessage];
    if(indexPath.row == self.requestArray.count-1)
    {
        [reviewCell.lineview removeFromSuperview];
    }
    
    return reviewCell;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
